<?php


namespace app\admin\controller;


use app\common\business\Category as CategoryBiz;
use think\facade\View;
use \app\admin\validate\Category as CategoryValidate;
use \app\common\model\mysql\Category as CategoryModel;

class Category extends AdminBase
{
    public function index() {
        $data = (new CategoryBiz)->getAllNormalList();
//        print_r($data);die;
        return View::fetch('', ['data'=>$data]);
    }

    /**
     * 添加分类
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(){

        if ($this->request->isPost()){
            $cat_name = $this->request->param('cat_name', '', 'trim');
            $parent_id = $this->request->param('parent_id', 0, 'intval');
            $type = $this->request->param('type', 0, 'intval');
            $data = [
                'cat_name' => $cat_name,
                'parent_id' => $parent_id,
                'type' => $type,
                'status' => 1,
                'operator' => $this->adminUser['username']
            ];
            //validate验证
            $validate = (new CategoryValidate)->scene('add');
            if (!$validate->check($data)) {
                return show(config('status.error'), $validate->getError());
            }
            if ((new CategoryModel)->add($data)) {
                return show(config('status.success'), '添加成功');
            }
            return show(config('status.error'), '添加失败');
        } else {
            $id= $this->request->param('id', 0, 'intval');
            //分类树
            $data = (new CategoryBiz())->getListTree();
            return View::fetch('', ['data'=>$data, 'id'=> $id]);
        }
    }
    public function edit() {
        $id = $this->request->param('id', 0, 'intval');
        if ($this->request->isPost()){
            $cat_name = $this->request->param('cat_name', '', 'trim');
            $parent_id = $this->request->param('parent_id', 0, 'intval');
            $type = $this->request->param('type', 0, 'intval');
            $data = [
                'id'=> $id,
                'cat_name' => $cat_name,
                'parent_id' => $parent_id,
                'type' => $type,
                'status' => 1,
                'operator' => $this->adminUser['username']
            ];
            //validate验证
            $validate = (new CategoryValidate)->scene('edit');
            if (!$validate->check($data)) {
                return show(config('status.error'), $validate->getError());
            }
            if (CategoryModel::update($data)) {
                return show(config('status.success'), '更新成功');
            }
            return show(config('status.error'), '更新失败');
        } else {
            if ($id == 0) {
                return false;
            }
            $info = (new CategoryBiz())->getById($id);
            //分类树
            $data = (new CategoryBiz())->getListTree();
            return View::fetch('', ['data'=>$data, 'info'=>$info]);
        }
    }

    public function listorder() {
        $id = input("param.id", 0, "intval");
        $listorder = input("param.listorder", 0, "intval");
        if (!$id || $listorder < 0) {
            return show(config('status.error'), "参数错误");
        }

        try {
            $res = (new CategoryBiz)->listorder($id, $listorder);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if($res) {
            return show(config('status.success'), "排序成功");
        } else {
            return show(config('status.error'), "排序失败");
        }
    }

    public function status() {
        $status = input("param.status", 0, "intval");
        $id = input("param.id", 0, "intval");
        if (!$id || !in_array($status, [0, 1])) {
            return show(config('status.error'), "参数错误");
        }

        try {
            $res = (new CategoryBiz())->status($id, $status);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if($res) {
            return show(config('status.success'), "状态更新成功");
        } else {
            return show(config('status.error'), "状态更新失败");
        }
    }

    /**
     * 添加商品是选择的一级分类
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function selectCategory() {
        //获取顶级分类
        $data = (new CategoryModel)->getFirstList();
        return view('', ['data'=> json_encode($data)]);
    }

    public function getchildrenByPid() {
        $parent_id = $this->request->param('parent_id', -1, 'intval');
        if ($parent_id == -1) {
            return show(config('status.error'), '参数错误');
        }
        $res = (new CategoryBiz())->getchildrenByPid($parent_id);
        if($res) {
            return show(config('status.success'), 'OK', $res);
        } else {
            return show(config('status.error'), "fail");
        }
    }

    public function delete() {
        $id = input("param.id", 0, "intval");
        if ($id == 0) {
            return show(config('status.error'), "参数错误");
        }
        if ((new CategoryModel)->where('id', $id)->delete()) {
            return show(config('status.success'), 'OK');
        }
        return show(config('status.error'), "fail");
    }

}